﻿using BST250301TestStand.Models;
using BST250301TestStand.Models.Utils;
using NPOI.SS.Formula.Functions;
using OpenTK.Audio.OpenAL;
using ScottPlot;
using ScottPlot.Colormaps;
using ScottPlot.Plottables;
using System.Diagnostics;
using System.Windows.Forms;
using static BST250301TestStand.Models.Utils.PLCConfig;
using static BST250301TestStand.Models.Utils.TCUConfig;


namespace BST250301.Pages
{
    public partial class Monitor : UserControl
    {
        private readonly Stopwatch sw = new();
        private static DataLogger loggerPLCSpeed;
        private static DataLogger loggerPLCTurque;
        private static DataLogger loggerTCUFBCF;
        private static DataLogger loggerTCUFBCR;
        private static DataLogger loggerTCUFBPTO;
        private static DataLogger loggerTCUBar;
        private static DataLogger loggerTCUTransmInSpeed;
        private static DataLogger loggerTCUTransmOutSpeed;
        private static DataLogger loggerTCUPTOSpeed;
        private static int source_select;
        public Monitor()
        {
            InitializeComponent();
            PLCConfig.ShowPLCStatus += ShowPLC;
            TCUConfig.ShowTCUStatus += ShowTCU;
            PlotDemo();
        }
        private void PlotDemo()
        {
            loggerPLCSpeed = formsPlot1.Plot.Add.DataLogger();
            loggerPLCSpeed.LineWidth = 2;
            loggerPLCSpeed.Color = Colors.AliceBlue;

            loggerPLCTurque = formsPlot1.Plot.Add.DataLogger();
            loggerPLCTurque.LineWidth = 2;
            loggerPLCTurque.Color = Colors.DodgerBlue;

            loggerTCUFBCF = formsPlot1.Plot.Add.DataLogger();
            loggerTCUFBCF.LineWidth = 2;
            loggerTCUFBCF.Color = Colors.Beige;

            loggerTCUFBCR = formsPlot1.Plot.Add.DataLogger();
            loggerTCUFBCR.LineWidth = 2;
            loggerTCUFBCR.Color = Colors.C4;

            loggerTCUFBPTO = formsPlot1.Plot.Add.DataLogger();
            loggerTCUFBPTO.LineWidth = 2;
            loggerTCUFBPTO.Color = Colors.DarkOliveGreen;

            loggerTCUTransmInSpeed = formsPlot1.Plot.Add.DataLogger();
            loggerTCUTransmInSpeed.LineWidth = 2;
            loggerTCUTransmInSpeed.Color = Colors.DarkOrange;

            loggerTCUTransmOutSpeed = formsPlot1.Plot.Add.DataLogger();
            loggerTCUTransmOutSpeed.LineWidth = 2;
            loggerTCUTransmOutSpeed.Color = Colors.White;

            loggerTCUPTOSpeed = formsPlot1.Plot.Add.DataLogger();
            loggerTCUPTOSpeed.LineWidth = 2;
            loggerTCUPTOSpeed.Color = Colors.Thistle;

            loggerTCUBar = formsPlot1.Plot.Add.DataLogger();
            loggerTCUBar.LineWidth = 2;
            loggerTCUBar.Color = Colors.Indigo;
            
            formsPlot1.Plot.Title("数据");
            formsPlot1.Plot.XLabel("Time(s)");
            formsPlot1.Plot.YLabel("rpm/mA/T/bar");

            sw.Start();
            formsPlot1.Plot.Font.Automatic();
        }
        /// <summary>
        /// 显示PLC在线信息
        /// </summary>
        /// <param name="isconnected"></param>
        /// <param name="Remotename"></param>
        private void ShowPLC(object? sender, ShowPLCStatusParm plc)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new EventHandler<ShowPLCStatusParm>(ShowPLC), sender, plc);
                return;
            }
            //double ms = sw.Elapsed.TotalSeconds;
            //loggerPLCSpeed.Add(ms, plc.DriverSpeed);
            //loggerPLCTurque.Add(ms, plc.LoadTorqut);

            //// 显示最近 5 秒
            ////formsPlot2.Plot.Axes.SetLimitsX(ms - 1, ms);
            //formsPlot1.Plot.Axes.SetLimitsX(0, ms);
            //this.Invoke(new MethodInvoker(() =>
            //{
            //    formsPlot1.Refresh();
            //}));
        }
        private void ShowTCU(object? sender, ShowTCUStatusParm tcu)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new EventHandler<ShowTCUStatusParm>(ShowTCU), sender, tcu);
                return;
            }
            //double ms = sw.Elapsed.TotalSeconds;
            //loggerTCUFBCF.Add(ms, tcu.CF_FBCurrent);
            //loggerTCUFBCR.Add(ms, tcu.CR_FBCurrent);
            //loggerTCUFBPTO.Add(ms, tcu.PTO_FBCurrent);
            //loggerTCUBar.Add(ms, tcu.SystemPressure);
            //loggerTCUTransmInSpeed.Add(ms, tcu.TransmInSpeed);
            //loggerTCUTransmOutSpeed.Add(ms, tcu.TransmOutSpeed);
            //loggerTCUPTOSpeed.Add(ms, tcu.PTOSpeed);

            //this.Invoke(new System.Windows.Forms.MethodInvoker(() =>
            //{
            //    formsPlot1.Refresh();
            //}));
        }
    }  
}